package cn.jhc.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.jhc.bean.Course;
import cn.jhc.dao.CourseDao;
import cn.jhc.util.HibernateUtil;

public class CourseDaoImpl implements CourseDao {
	private static final Logger LOG = Logger.getLogger(CourseDaoImpl.class.getName());

	@Override
	public void save(Object o) {
		if (o instanceof Course) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("保存失败，请传入 " + Course.class.getName() + "的实例对象");
	}

	@Override
	public void update(Object o) {
		if (o instanceof Course) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("更新失败，请传入 " + Course.class.getName() + "的实例对象");
	}

	@Override
	public void delete(Object o) {
		if (o instanceof Course) {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(o);
			session.getTransaction().commit();
			return;
		}
		LOG.info("删除失败，请传入 " + Course.class.getName() + "的实例对象");
	}

	@Override
	public Object get(int id) {
		Course course = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		course = session.get(Course.class, id);
		session.getTransaction().commit();
		return course;
	}

	@Override
	public List list() {
		String hql = "from Course";
		List<Course> courses = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		courses = query.list();
		session.getTransaction().commit();
		return courses;
	}

	@Override
	public List list(int start, int count) {
		String hql = "from Course";
		List<Course> courses = null;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(start + count);
		courses = query.list();
		session.getTransaction().commit();
		return courses;
	}

	@Override
	public long getCount() {
		String hql = "select count(c.courseno) from Course c";
		Long count = 0l;
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		count = (Long)query.uniqueResult();
		session.getTransaction().commit();
		return count;
	}
	
	@Override
	public List<Course> list(String name) {
		String hql = "from Course c where c.coursename like ?";
		List<Course> courses = null;
		
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		query.setString(0, "%" + name + "%");
		courses = query.list();
		session.getTransaction().commit();
		
		
		return courses;
	}

}
